﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ovvio.Data;
using System.Threading;

namespace Ovvio.Business
{
    public class UserService : ServiceBase<int,User,OvvioDataContext>
    {
        public UserService() : base( p => p.Id ) { }

        private int defaultRecursionLevel = -1;

        public User GetUserWithDownline(int id)
        {
            using (var ctx = new OvvioDataContext())
            {
                var query = from i in CreateQuery(ctx)
                            where i.Id == id
                            select i;

                if (query.Any())
                {
                    var user = query.First();
                    Recurse(user,0);
                    return user;
                }
                
            }
            return null;
        }

        public User GetUserWithUpline(int id)
        {
            using (var ctx = new OvvioDataContext())
            {
                var query = from i in CreateQuery(ctx)
                            where i.Id == id
                            select i;

                if (query.Any())
                {
                    var user = query.First();
                    user.UplineReference.Load();
                    return user;
                }

            }
            return null;
        }

        private void Recurse(User user, int level)
        {
            if (defaultRecursionLevel != -1)
            {
                level++;
                if (level >= defaultRecursionLevel)
                    return;
            }

            user.Downline.Load();
            if (user.Downline.Any())
                foreach(var u in user.Downline)
                    Recurse(u, level);
        }

        public static Role GetUserRole(string username)
        {
            using (var ctx = new OvvioDataContext())
            {
                var query = from i in ctx.Users
                            where i.Username.ToUpper().Equals(username.ToUpper())
                            select i;

                if (query.Any())
                    return (Role)query.First().Role;
            }
            return Role.Guest ;
        }

        protected override void OnEntityCreate(User entity)
        {
            entity.Role = 0;
            entity.IsActive = true;
        }

        protected override void OnEntityBeforeSave(User entity)
        {
            entity.Username = entity.Username.ToUpper();
            entity.UpdatedBy = Thread.CurrentPrincipal.Identity.Name;
            var user = GetEntityByKey(entity.Id);
            if (user != null)
            {
                entity.RegistrationDate = user.RegistrationDate;
                entity.LastLogonDate = user.LastLogonDate;
            }
            else
                entity.RegistrationDate = DateTime.Now;
            base.OnEntityBeforeSave(entity);
        }

    }

}
